package com.example.a2032.activity;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.a2032.R;
import com.example.a2032.Utils.ClockUtils;
import com.example.a2032.adapter.ClockAdapter;
import com.example.a2032.entity.JstyleClock;
import com.example.a2032.viewmodel.ClockDataViewModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ClockListActivity extends BaseActivity {

    @BindView(R.id.iv_title)
    ImageView ivTitle;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.RecyclerView_clock)
    RecyclerView RecyclerViewClock;
    private AlarmManager aManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clock_list);
        ButterKnife.bind(this);
        init();
    }

    ClockAdapter clockAdapter;

    private void init() {
        aManager = (AlarmManager) getSystemService(Service.ALARM_SERVICE);
        String title = getIntent().getStringExtra(SettingActivity.KEY_SettingTitle);
        tvTitle.setText(title);
        ClockDataViewModel clockDataViewModel = new ViewModelProvider(this).get(ClockDataViewModel.class);
        clockDataViewModel.getAllClock().observe(this, new Observer<List<JstyleClock>>() {
            @Override
            public void onChanged(List<JstyleClock> jstyleClocks) {
                if (jstyleClocks != null && jstyleClocks.size() != 0) {
                    clockAdapter.updateData(jstyleClocks);
                }else{
                    clockAdapter.clear();
                }
            }
        });
       final List<JstyleClock> list = new ArrayList<>();
//        list.add(new JstyleClock(7, 0, true, 1,"true",1));
//        list.add(new JstyleClock(8, 0, true, 2,"true",2));
//        list.add(new JstyleClock(9, 0, true, 3,"true",3));
//        list.add(new JstyleClock(10, 0, true, 4,"true",4));
//        list.add(new JstyleClock(11, 0, true, 0,"true",5));
//        list.add(new JstyleClock(12, 0, true, 0,"true",6));
        clockAdapter = new ClockAdapter(list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        RecyclerViewClock.setLayoutManager(linearLayoutManager);
        RecyclerViewClock.setAdapter(clockAdapter);
//        setClock(1, JstyleClock.CLOCK_DRINK, System.currentTimeMillis() + 2000);
//        setClock(2, JstyleClock.CLOCK_FOOD, System.currentTimeMillis() + 5000);
//        cancelClock(1);
        // cancelClock(1);
    }
    void showClockActivity( JstyleClock jstyleClock){


        Intent intentClock=new Intent(this, ClockNotifyActivity.class);
        intentClock.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intentClock.putExtra(ClockNotifyActivity.KEY_ClockType, jstyleClock.getClockType());
        intentClock.putExtra(ClockUtils.JSTYLE_CLOCK, jstyleClock);
       startActivity(intentClock);
    }
    @OnClick(R.id.iv_title)
    public void onViewClicked() {
        finish();
    }

    void setClock(int id, int clockType, long time) {

        Intent intent = new Intent("android.intent.action.ALARM_RECEIVER");
        intent.addCategory("android.intent.category.jstyleWatch");
        intent.putExtra(ClockNotifyActivity.KEY_ClockType, clockType);
        //intent.putExtra("clock", new JstyleClock(7, 0, true, 0));
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        aManager.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent);
    }

    void cancelClock(int id) {
        Intent intent = new Intent("android.intent.action.ALARM_RECEIVER");
        intent.addCategory("android.intent.category.jstyleWatch");
        aManager.cancel(PendingIntent.getBroadcast(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT));
    }
}